package com.example.klaraservice.controller;

import com.example.klaraservice.pojo.Cart;
import com.example.klaraservice.service.CartService;
import com.example.klaraservice.vo.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author : Glacialerisa
 * @date : 2023/6/10 13:43
 * @desc :
 */
@Api(tags = "购物车")
@RestController
@RequestMapping("/api/cart")
public class CartController {
    @Resource
    private CartService cartService;
    @ApiOperation("获取用户的所有的购物车")
    @GetMapping("/getCarts")
    public R getCarts(@RequestParam int id){
        List<Cart> carts = cartService.getCarts(id);
        if (carts!=null){
            return R.ok().data("carts", carts);
        }
        return R.error();
    }
    @ApiOperation("获取某一购物车")
    @GetMapping("/getCart")
    public R getCart(@RequestParam int id){
        Cart cart = cartService.getCart(id);
        if (cart!=null){
            return R.ok().data("cart", cart);
        }
        return R.error();
    }
//    @ApiOperation("修改购物车信息")
//    @PutMapping("/updateCart")
//    public R updateCart(@RequestBody Cart cart){
//        boolean result = cartService.updateCart(cart);
//        if (result){
//            return R.ok();
//        }
//        return R.error();
//    }
    @ApiOperation("修改购物车信息")
    @PutMapping("/updateCart")
    public R updateCart(@RequestBody List<Cart> carts){
        boolean result = false;
        for (Cart cart:carts
             ) {
            result = cartService.updateCart(cart);
        }
//        boolean result = cartService.updateCart(cart);
        if (result){
            return R.ok();
        }
        return R.error();
    }
    @ApiOperation("删除购物车")
    @DeleteMapping("/deleteCart")
    public R deleteCartById(@RequestParam int id){
        boolean result = cartService.deleteCartById(id);
        if (result){
            return R.ok();
        }
        return R.error();
    }
}
